﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bricks.BaseFramework.GenericForms;

namespace Bricks.BaseFramework.Utility
{
    public class ExceptionHelper
    {
        /// <summary>
        /// 处理异常
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="customerMessage">自定义错误信息</param>
        /// <param name="alertUser">是否提示用户发生异常</param>
        /// <param name="alertUserDetailMsg">
        /// 是否提示用户异常详细信息,如果为False则当customerMessage存在时只提示customerMessage
        /// 当customerMessage不存在时将提示系统预定义错误项
        /// </param>
        public static void HandleException(Exception ex, string customerMessage, bool alertUser, bool alertUserDetailMsg)
        {
            //Log.LogService.LogWriter.WriteException(string.IsNullOrWhiteSpace(customerMessage) ? "未处理的异常" : customerMessage, ex);
            string simpleFriendlyAlterMsg = string.Empty;

            StringBuilder errorDetailMsg = new StringBuilder();
            if (!String.IsNullOrWhiteSpace(customerMessage))
            {
                errorDetailMsg.AppendLine(customerMessage);
                simpleFriendlyAlterMsg = customerMessage;
            }

            errorDetailMsg.AppendLine("[异常信息]：");
            errorDetailMsg.AppendLine(ex.Message);
            Exception exectEx = ex;
            while (exectEx.InnerException != null)
            {
                errorDetailMsg.AppendLine(exectEx.Message);
                exectEx = exectEx.InnerException;
            }

            if (String.IsNullOrWhiteSpace(simpleFriendlyAlterMsg))
            {
                if (exectEx is ArgumentException)
                {
                    simpleFriendlyAlterMsg = exectEx.Message;
                }
                else
                {
                    simpleFriendlyAlterMsg = "系统异常";
                }

            }

            if (alertUser && !alertUserDetailMsg)
            {
                simpleFriendlyAlterMsg = simpleFriendlyAlterMsg + Environment.NewLine + "请重新尝试或联系系统管理员";
                MsgBox.Show(simpleFriendlyAlterMsg, "操作失败", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                return;
            }

            if (alertUser && alertUserDetailMsg)
            {
                errorDetailMsg.AppendLine("请重新尝试或联系系统管理员");
                MsgBox.Show(errorDetailMsg.ToString(), "操作失败", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                return;
            }
        }

        /// <summary>
        /// 默认将不向用户提示详细的错误信息
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="customerMessage"></param>
        public static void HandleException(Exception ex, string customerMessage)
        {
            HandleException(ex, customerMessage, true, true);
        }

        public static void HandleException(Exception ex)
        {
            HandleException(ex, String.Empty, true, true);
        }

        /// <summary>
        /// 是否触发唯一约束异常
        /// </summary>
        /// <param name="ex">异常实体</param>
        /// <returns></returns>
        public static bool IsUniqueConstraintError(Exception ex)
        {
            bool isUniqueError = false;
            //var sqlException = ex as SqlException;
            //if (null == sqlException)
            //{
            //    if (null != ex.InnerException)
            //    {
            //        isUniqueError = IsUniqueConstraintError(ex.InnerException);
            //    }
            //}
            //else
            //{
            //    //唯一约束异常代码
            //    if (sqlException.Number == 2601)
            //    {
            //        isUniqueError = true;
            //    }
            //}
            return isUniqueError;
        }
    }
}
